<template>
  <div class="box">
    <h2>Son1 子组件 演示原生的写法</h2>
    从vuex中获取的值: {{this.$store.state.count}}<label></label>
    <br>
    <button @click="addCount(1)">值 + 1</button>
    <button @click="addCount(5)">值 + 5</button>
    <button @click="addCount(10)">值 + 10</button>
    <button @click="changeNum">一秒后修改成666</button>
    <button @click="updateTitle">改标题</button>

    <hr>
    <!-- 计算属性getters -->
    <div>{{$store.state.list}}</div>
    <div>{{$store.getters.getList}}</div>

    <hr>
    <!-- 测试访问模块中的state - 原生 -->
    <div>{{$store.state.user.name}}</div>
    <button @click="updateUser" >更新个人信息</button>
    <button @click="updateUser2">一秒后更新信息</button>

    <div></div>
    <button >更新主题色</button>
    <hr>
    <!-- 测试访问模块中的getters - 原生 -->
    <div></div>

  </div>
</template>

<script>

export default {

  methods:{
    addCount(n){
      // this.$store.state.count+=n
      this.$store.commit('addCount', n)
    },
    changeNum(){
      this.$store.dispatch('changeNum', 666)
    },
    updateTitle(){
      this.$store.state.title = '修改的新的标题'
    },
    updateUser(){
      this.$store.commit('updateUser', {name:'老六', age:26})
    },
    updateUser2(){
      this.$store.commit('updateUser2', {name:'老六1秒更新', age:30})
    }
  },
  computed:{
    getList(){
      return this.$store.getters.getList();
    }
  }

}
</script>

<style lang="css" scoped>
.box{
  border: 3px solid #ccc;
  width: 400px;
  padding: 10px;
  margin: 20px;
}
h2 {
  margin-top: 10px;
}
</style>
